package org.dromara.yuanshidian.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.yuanshidian.domain.bo.ExamineDataBo;
import org.dromara.yuanshidian.domain.vo.ExamineDataVo;
import org.dromara.yuanshidian.domain.ExamineData;
import org.dromara.yuanshidian.mapper.ExamineDataMapper;
import org.dromara.yuanshidian.service.IExamineDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 审核列表Service业务层处理
 *
 * @author Lion Li
 * @date 2025-02-09
 */
@RequiredArgsConstructor
@Service
public class ExamineDataServiceImpl implements IExamineDataService {

    private final ExamineDataMapper baseMapper;

    /**
     * 查询审核列表
     *
     * @param id 主键
     * @return 审核列表
     */
    @Override
    public ExamineDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询审核列表列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 审核列表分页列表
     */
    @Override
    public TableDataInfo<ExamineDataVo> queryPageList(ExamineDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamineData> lqw = buildQueryWrapper(bo);
        Page<ExamineDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的审核列表列表
     *
     * @param bo 查询条件
     * @return 审核列表列表
     */
    @Override
    public List<ExamineDataVo> queryList(ExamineDataBo bo) {
        LambdaQueryWrapper<ExamineData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamineData> buildQueryWrapper(ExamineDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamineData> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExaminedataId() != null, ExamineData::getExaminedataId, bo.getExaminedataId());
        lqw.like(StringUtils.isNotBlank(bo.getDwname()), ExamineData::getDwname, bo.getDwname());
        lqw.like(StringUtils.isNotBlank(bo.getRyname()), ExamineData::getRyname, bo.getRyname());
        lqw.eq(StringUtils.isNotBlank(bo.getDwcode()), ExamineData::getDwcode, bo.getDwcode());
        lqw.eq(StringUtils.isNotBlank(bo.getYwcode()), ExamineData::getYwcode, bo.getYwcode());
        lqw.eq(StringUtils.isNotBlank(bo.getGrid()), ExamineData::getGrid, bo.getGrid());
        lqw.eq(bo.getExaminedataType() != null, ExamineData::getExaminedataType, bo.getExaminedataType());
        lqw.eq(bo.getExamineStatus() != null, ExamineData::getExamineStatus, bo.getExamineStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getExamineRemark()), ExamineData::getExamineRemark, bo.getExamineRemark());
        lqw.like(StringUtils.isNotBlank(bo.getExamineName()), ExamineData::getExamineName, bo.getExamineName());
        lqw.eq(bo.getExamineDate() != null, ExamineData::getExamineDate, bo.getExamineDate());
        return lqw;
    }

    /**
     * 新增审核列表
     *
     * @param bo 审核列表
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ExamineDataBo bo) {
        ExamineData add = MapstructUtils.convert(bo, ExamineData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改审核列表
     *
     * @param bo 审核列表
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ExamineDataBo bo) {
        ExamineData update = MapstructUtils.convert(bo, ExamineData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamineData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除审核列表信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
